/* ========================================================================
 * PROJECT: FTIR Blob Tracker
 * ========================================================================
 * This work is based on the original FTIR Blob Tracker developed by
 *   Woonhyuk Baek
 *   Woontack Woo
 *   U-VR Lab, GIST of Gwangju in Korea.
 *   
 *   http://uvr.gist.ac.kr/
 *
 * Copyright of the derived and new portions of this work
 *     (C) 2009 GIST U-VR Lab.
 *
 * This framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For further information please contact 
 *   Woonhyuk Baek
 *   <windage@live.com>
 *   GIST U-VR Lab.
 *   Department of Information and Communication
 *   Gwangju Institute of Science and Technology
 *   1, Oryong-dong, Buk-gu, Gwangju
 *   South Korea
 * ========================================================================
 ** @author   Woonhyuk Baek
 * ======================================================================== */

#ifndef DRAW_INFORMATION_WRAPPING_H
#define DRAW_INFORMATION_WRAPPING_H

void DrawResultInformation(IplImage* resultImage, std::vector<Blob>* trackedBlobList, bool trackingInfo = false, bool SHOW_PROCESS = false)
{
	char message[100];

	for(unsigned int i=0; i<trackedBlobList->size(); i++)
	{
		(*trackedBlobList)[i].Draw(resultImage);
		BlobPoint center = (*trackedBlobList)[i].pt;
		
		if(SHOW_PROCESS || trackingInfo)
		{
			char eventMessage[100];
			switch((*trackedBlobList)[i].eventMessage)
			{
			case Blob::MEMSSAGE_NONE: sprintf_s(eventMessage, "NONE"); break;
			case Blob::MEMSSAGE_ADD:  sprintf_s(eventMessage, "ADD");  break;
			case Blob::MEMSSAGE_MOVE: sprintf_s(eventMessage, "MOVE"); break;
			case Blob::MEMSSAGE_HOLD: sprintf_s(eventMessage, "HOLD"); break;
			}

			sprintf_s(message, "ID:%d(%0.2fs) - %s", (*trackedBlobList)[i].id, (*trackedBlobList)[i].age/1000.0f, eventMessage);
			DrawTextToImage(resultImage, cvPoint((int)center.x + 5, (int)center.y + 10), message);
		}
		else
		{
			CvScalar color = (*trackedBlobList)[i].color;
			double angle = (*trackedBlobList)[i].angle;
			CvRect outerBox = (*trackedBlobList)[i].boundingRect;

			center.x -= outerBox.width/2;
			center.y += outerBox.height/2;
			cvLine(resultImage, cvPoint((int)center.x, (int)center.y + 2), cvPoint((int)center.x + (int)((*trackedBlobList)[i].age/1000.0f), (int)center.y + 2), color, 3);
		}
	}

	sprintf_s(message, "Press 'Q' key to terminate program");
	DrawTextToImage(resultImage, cvPoint(5, 15), message);
	sprintf_s(message, "Press 'G' key to update bacground image");
	DrawTextToImage(resultImage, cvPoint(5, 30), message);
	sprintf_s(message, "Press 'I' key to toggle the tracking info");
	DrawTextToImage(resultImage, cvPoint(5, 45), message);
}

void CreateWindows(Pipelining* pipeline)
{
	cvNamedWindow("input image");
	cvNamedWindow("blob detection");
	for(int i=0; i<pipeline->GetSize(); i++)
		cvNamedWindow(pipeline->GetFilterName(i).c_str());
}

void ShowPipelineImage(Pipelining* pipeline)
{
	for(int i=0; i<pipeline->GetSize(); i++)
	{
		Filter* filter = pipeline->GetFilter(i);
		cvShowImage(filter->GetName().c_str(), filter->GetOutputImage());
	}
}

void ShowBlobImage(IplImage* tempImage, std::vector<Blob>* newBlob)
{
	cvSetZero(tempImage);
	CvRNG rng = cvRNG(cvGetTickCount());

	for(unsigned int i=0; i<newBlob->size(); i++)
	{
		CvRect rect = (*newBlob)[i].boundingRect;
		CvScalar color = CV_RGB(cvRandInt(&rng)%255, cvRandInt(&rng)%255, cvRandInt(&rng)%255);
		cvRectangle(tempImage, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), color, CV_FILLED);
	}
	cvShowImage("blob detection", tempImage);
}

#endif